package com.example.librarymanage.controller;

import com.example.librarymanage.model.UserInfo;
import com.example.librarymanage.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class LoginController {
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {

        if(!StringUtils.hasLength(userName)
                || !StringUtils.hasLength(password)
                ) {
            return false;
        }
        UserInfo userInfo = userInfoService.selectByName(userName);
        if(null == userInfo) return false;
        if(userInfo.getPassword().equals(password)) {
            session.setAttribute("userName",userName);
            return true;
        }
        return false;

    }

    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session) {
        String userName = (String)session.getAttribute("userName");
        if(StringUtils.hasLength(userName)) {
            return userName;
        } else {
            return "";
        }
    }
}
